package cn.finedo.aiability.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import cn.finedo.aiability.exception.AiAbilityException;

public class Base64Util {

	public static String getBase64String(InputStream in) throws Exception {
		byte[] data = null;
		// 读取图片数组
		try {
			ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
			byte[] buff = new byte[256];
			int rc = 0;
			while ((rc = in.read(buff, 0, 256)) > 0) {
				swapStream.write(buff, 0, rc);
			}
			data = swapStream.toByteArray();
		} catch (IOException e) {
			throw new AiAbilityException(e, e.getMessage());
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					throw new AiAbilityException(e, e.getMessage());
				}
			}
		}
		return Base64.getEncoder().encodeToString(data);
	}
}
